import axios from 'axios';
import router from '../router'; // 假设你的路由器实例被导入为 router

// 创建 Axios 实例
const axiosInstance = axios.create({
  // baseURL: 'http://124.220.40.115:82',
  timeout: 5000,
});

// 请求拦截器
axiosInstance.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token');

    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

axiosInstance.interceptors.response.use(
  (response) => {
    if (response.data.code === 401) {
      router.push('/');
    }
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 定义接口
const api = {

  login: (data) => axiosInstance.post('/interface/login', data),
  getprocess: () => axiosInstance.get('/interface/shasha/info/process', ),
  getstudyId: (data) => axiosInstance.get('/interface/shasha/study/studyId', {
    params: data
  }),
  gettestId: (data) => axiosInstance.get('/interface/shasha/bank/testId', {
    params: data
  }),
  accuracy: (data) => axiosInstance.post('/interface/shasha/accuracy', data),
  addachieve: (data) => axiosInstance.post('/interface/shasha/achieve', data),
  addrecord: (data) => axiosInstance.post('/interface/shasha/record', data),
  putprocess: (data) => axiosInstance.put('/interface/shasha/info/process', data),
  getfiveques: () => axiosInstance.get('/interface/shasha/ques'),
  addfiveques: (data) => axiosInstance.post('/interface/shasha/part5/record', data),
  getachieve: () => axiosInstance.get('/interface/shasha/achieve'),
  getinfo: () => axiosInstance.get('/interface/shasha/info'),
  putinfo: (data) => axiosInstance.put('/interface/shasha/info',data),
  addinfo: (data) => axiosInstance.post('/interface/shasha/info',data),
  getlist: (data) => axiosInstance.get('/interface/shasha/accuracy/list',{params:data}),
  getconsolidate:(data)=> axiosInstance.get('/interface/shasha/bank/consolidate',{params:data}),
  addconsolidate:(data)=> axiosInstance.post('/interface/shasha/record/consolidate',data),
  getonebank:(data)=> axiosInstance.get(`/interface/shasha/bank/${data}`),
  // getconsolidate:(data)=> axiosInstance.post('/interface/shasha/record/consolidate',data),
};

export default api;